package impl

import (
	"context"
	"fmt"
	"time"

	"gitee.com/yangwencheng/vblog/apps/blog"
	"gitee.com/yangwencheng/vblog/common"
	"gorm.io/gorm"
)

// 查询列表
func (impl *Impl) QueryBlog(ctx context.Context, qbr *blog.QueryBlogRequest) (*blog.BlogSet, error) {

	query := impl.db.Session(&gorm.Session{})
	if qbr.Title != "" {
		query = query.Where("title like ?", fmt.Sprintf("%%%s%%", qbr.Title))
	}
	if qbr.Author != "" {
		query = query.Where("auth like ?", fmt.Sprintf("%%%s%%", qbr.Author))
	}
	if qbr.Status != 0 {
		query = query.Where("status = ?", qbr.Status)
	}

	if !qbr.PublishedAtStart.IsZero() {
		query = query.Where("published_at >= ?", qbr.PublishedAtStart.Unix())
	}

	if !qbr.PublishedAtEnd.IsZero() {
		query = query.Where("published_at <= ?", qbr.PublishedAtEnd.Unix())
	}

	blogSet := blog.NewBlogSet()
	result := query.Model(&blog.Blog{}).Count(&qbr.Total).Scopes(common.Paginate(qbr.PageData)).Order("id DESC").Find(&blogSet.Items)
	blogSet.PageData = qbr.PageData
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}

	return blogSet, nil
}

// 创建
func (impl *Impl) CreateBlog(ctx context.Context, cbr *blog.CreateBlogRequest) (*blog.Blog, error) {
	err := cbr.Validate()
	if err != nil {
		return nil, common.NewApiException(common.PARAMS_ERROR_CODE, err.Error())
	}
	blog := blog.NewBlog()
	blog.CreateBlogRequest = cbr
	result := impl.db.Create(blog)
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}
	return blog, nil
}

// 更新
func (impl *Impl) UpdateBlog(ctx context.Context, cbr *blog.CreateBlogRequest, blog *blog.Blog) (*blog.Blog, error) {
	err := cbr.Validate()
	if err != nil {
		return nil, common.NewApiException(common.PARAMS_ERROR_CODE, err.Error())
	}
	result := impl.db.First(blog)
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}
	blog.CreateBlogRequest = cbr
	blog.UpdatedAt = time.Now().Unix()
	result = impl.db.Save(blog)
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}
	return blog, nil
}

// 删除
func (impl *Impl) DeleteBlog(cxt context.Context, delBlogReq *blog.DeleteBlogRequest) (*blog.Blog, error) {

	if err := delBlogReq.Validate(); err != nil {
		return nil, common.NewApiException(common.PARAMS_ERROR_CODE, err.Error())
	}
	blog := blog.NewBlog()
	blog.ID = delBlogReq.ID
	result := impl.db.Delete(blog)
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}
	if result.RowsAffected == 0 {
		return nil, common.NewApiException(common.PARAMS_ERROR_CODE, "数据不存在")
	}
	return blog, nil
}

// 详情
func (impl *Impl) ShowBlog(ctx context.Context, showBlogReq *blog.ShowBlogRequest) (*blog.Blog, error) {

	if err := showBlogReq.Validate(); err != nil {
		return nil, common.NewApiException(common.PARAMS_ERROR_CODE, err.Error())
	}
	blog := blog.NewBlog()
	blog.ID = showBlogReq.ID
	result := impl.db.First(blog)
	if result.Error != nil {
		return nil, common.NewApiException(common.DATABASE_ERROR_CODE, result.Error.Error())
	}
	return blog, nil
}
